package ui.scene
{
	import core.actor.core.ActorView;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import manager.CharManager;
	import manager.LoadManager;
	import manager.SceneManager;
	
	import map.GameMap;
	import map.MapInfo;
	
	import resource.LoadResource;
	
	public class MainScene extends Sprite
	{
		protected var _tickType:int = 1;//重写
		
		public var gameMap:GameMap;//地图
		
		//当前地面的偏移
		public var dx:int,dy:int;
		//上次地面的偏移
		private var lastdx:int,lastdy:int;
		
		public function MainScene()
		{
			super();
			TickManager.instance.addEventListener(TickEvent.TICK,update);
			//this.cacheAsBitmap = true;
			//init();
		}
		
		public function get tickType():int
		{
			return _tickType;
		}
		
		//private var loader:Loader = new Loader();
		private function init():void
		{
			//地图
			gameMap = new GameMap(this);
			
			var playerBoneUrl:String = "../testRes/TestBoneRes.swf";
			//CharManager.getInstance().setGamePlayer(0,playerBoneUrl);
			
			//addOtherTestPlayer();
			
			addWorldEventListener();
		}
		
		public function addOtherTestPlayer():void{
			
			for(var i:int=0;i<50;i++)
			{
				var otherPlayerBoneUrl:String = "../testRes/TestBoneRes.swf";
				CharManager.getInstance().addCharacter(i+1000,otherPlayerBoneUrl,ActorView.CHARACTER_OTHER_PLAYER);
			}
		}
		
		public function initGameMap():void{
			//初始地图尺寸等
			MapInfo.width = 2100;
			MapInfo.height = 1200;
			MapInfo.resId = 9999;
			MapInfo.init();
			
			
			init();
			//小地图（地面缩略图）
			gameMap.smallMap = LoadResource.getSmallMap("small_map_"+MapInfo.resId+".png");
			//地图所有层的配置文件
			LoadManager.loadMapXml(LoadManager.resourcePath + "testRes/map/"+MapInfo.resId+"/"+MapInfo.resId+".xml");
			
			dx = dy = 0;
			gameMap.setMapOffset(dx,dy);
		}
		
		public function addWorldEventListener():void{
			stage.addEventListener(MouseEvent.MOUSE_DOWN,SceneManager.getInstance().cursorManager.sceneMouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP,SceneManager.getInstance().cursorManager.sceneMouseUpHandler);
		}
		
		//-----------------地图运动相关-------------------
		
		public function update(e:TickEvent):void//重写
		{
			CharManager.getInstance().testOtherCharacterMove();
			move(getTimer());
		}
		
		private var speedTime:int;
		/**
		 *  GameWorld->enterFrame ----> MainScene->move
		 *  世界运动，包括人物及其他玩家的运动, 地图的偏移 
		 * @param curTime 当前时间
		 */		
		public function move(curTime:int):void {
			//玩家运动速度
			MapInfo.MOVE_SPEED = (curTime-speedTime) * MapInfo.PRE_SPEED;
			MapInfo.MOVE_SPEED = Math.min(MapInfo.MOVE_SPEED, 20);
			
			CharManager.getInstance().moveOtherPlayer();//移动其他玩家
			
			var isMoveEnd:Boolean = CharManager.getInstance().gamePlayer.move();//先得到主角在世界的偏移,并判断主角是否運動完畢
			worldMove(isMoveEnd);
			
			speedTime = curTime;
		}
		
		//世界运动	 		
		public function worldMove(isEnd:Boolean):void {			
			//当玩家停止运动时地图停止计算
			if(!isEnd && revOffset()){
				gameMap.setMapOffset(dx,dy);
			}
		}
		
		//修正偏移
		private function revOffset():Boolean {
			//修正地图偏移量
			if(dx < 0 ){
				dx=0;
			}else if(dx>(MapInfo.width-MapInfo.view_width)){
				dx=MapInfo.width-MapInfo.view_width;
			}
			if(dy<0){
				dy=0;
			}else if(dy>(MapInfo.height-MapInfo.view_height)){
				dy=MapInfo.height-MapInfo.view_height;
			}
			
			//避免重复计算偏移
			if(dx == lastdx && dy == lastdy){
				return false;
			}
			
			this.lastdx = dx;
			this.lastdy = dy;
			
			return true;
		}
		
		public function destory():void{
			this.gameMap.destory();
			this.lastdx = this.lastdy = -1;
			this.dx = this.dy = -99;
		}
	}
}